<?php
/**
 * Here is your custom functions.
 */

use support\Response;

/**
 * @param mixed $data
 * @param array $extend
 * @return Response
 */
function result($data, array $extend = []): Response
{
    return assemble_response(1, 'ok', $data, $extend);
}

/**
 * @param string $msg
 * @param mixed $data
 * @param array $extend
 * @return Response
 */
function success(string $msg, $data = null, array $extend = []): Response
{
    return assemble_response(1, $msg, $data, $extend);
}

/**
 * @param int $code
 * @param string $msg
 * @param array $extend
 * @return Response
 */
function error(string $msg, int $code = 0, array $extend = []): Response
{
    return assemble_response($code, $msg, [], $extend);
}

/**
 * @param int $code
 * @param string $msg
 * @param mixed $data
 * @param array $extend
 * @return Response
 */
function assemble_response(int $code, string $msg, $data = [], array $extend = []): Response
{
    return json(array_merge([
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
        'time' => time(),
    ], $extend));
}
